package com.seeyon.apps.kk.util;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.util.FlipInfo;
import com.seeyon.ctp.util.JDBCAgent;
import com.seeyon.ctp.util.UUIDLong;

public class JdbcAgentUtil {

	private static final Log log = LogFactory.getLog(JdbcAgentUtil.class);

	public static List<Map> doQuery(String str, Map params) throws BusinessException {
		JDBCAgent agent = new JDBCAgent();
		try {
			if (MapUtils.isEmpty(params)) {
				agent.execute(str);
			} else {
				agent.executeNamedSql(str, params);
			}
			return agent.resultSetToList();
		} catch (Exception e) {
			log.error("#客开日志：数据查询失败!", e);
			throw new BusinessException(e);
		} finally {
			if (agent != null) {
				agent.close();
			}
		}
	}
	
	public static FlipInfo doQuery(String str, Map params, FlipInfo flipInfo) throws BusinessException {
		JDBCAgent agent = new JDBCAgent();
		try {
			return agent.findNameByPaging(str, params, flipInfo);
		} catch (Exception e) {
			log.error("#客开日志：数据查询失败!", e);
			throw new BusinessException(e);
		} finally {
			if (agent != null) {
				agent.close();
			}
		}
	}

	public static void doUpdateOrInsert(String sql, List list) throws BusinessException {
		JDBCAgent jdbc = new JDBCAgent();
		try {
			jdbc.batch1Prepare(sql);
			if(list != null){
				jdbc.batch2Add(list);
			}
			int count = jdbc.batch3Execute();
			log.info("#强生客开SQL doUpdateOrInsert:" + count + "条！");
		} catch (BusinessException e) {
			log.error("#客开日志：数据更新失败!", e);
			throw new BusinessException(e);
		} catch (SQLException e) {
			log.error("#客开日志：数据更新失败!", e);
			throw new BusinessException(e);
		} finally {
			if (jdbc != null) {
				jdbc.close();
			}
		}
	}

	public static Long getUUID() {
		return UUIDLong.longUUID();
	}
}
